/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.packet.shared;

import java.util.Collection;
import java.util.Iterator;

import lineage.controller.pc.L1PcController;
import lineage.core.L1GameClient;
import lineage.model.instance.L1PcInstance;
import lineage.packet.L1AbstractPacketClient;
import lineage.packet.server.S_CharSelectCount;
import lineage.packet.server.S_CharSelectInfo;
import lineage.util.DateTimeUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 客户端与服务端共用：显示角色列表.
 * 
 * @version 2014年3月15日下午1:52:40
 * @author jrwz
 */
public final class CS_CharList extends L1AbstractPacketClient {
    private static final Logger LOG = LoggerFactory.getLogger(CS_CharList.class);

    @Override
    public void execute(final L1GameClient client, final byte[] data) {
        try {
            deltetChar(client); // 指定的删除时间到 删除角色

            final int count = client.getAccount().getCharCount();
            client.sendPacket(new S_CharSelectCount(count)); // 发送角色列表

            if (count > 0) { // 如果已创角色则发送该帐号下所有已存在的角色信息
                sendAllCharMessage(client);
            }
        } catch (final Exception e) {
        } finally {
            this.close();
        }
    }

    /**
     * 删除角色.
     * 
     * @param client
     *            - 客户端对象
     */
    private void deltetChar(final L1GameClient client) {
        final String accountName = client.getAccountName();
        try {
            final Collection<L1PcInstance> pc = L1PcController.getList();
            final Iterator<L1PcInstance> iter = pc.iterator();
            while (iter.hasNext()) {
                final L1PcInstance tgPc = iter.next();
                if (tgPc.getAccountName().equalsIgnoreCase(accountName)) {
                    final long deleteTime = tgPc.getDeleteTime().getTime();
                    final long nowTime = DateTimeUtil.getTimestampMillis().getTime(); // 当前时间
                    if (deleteTime > 0 && deleteTime < nowTime) {
                        iter.remove(); // 删除元素
                        L1PcController.delCharacters(client, tgPc); // 删除角色
                    }
                }
            }
        } catch (final Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    }

    /**
     * 发送该帐号下所有已有角色的信息.
     * 
     * @param client
     *            - 客户端对象
     */
    public void sendAllCharMessage(final L1GameClient client) {
        final String accountName = client.getAccountName();
        try {
            final Collection<L1PcInstance> pc = L1PcController.getList();
            final Iterator<L1PcInstance> iter = pc.iterator();
            while (iter.hasNext()) {
                final L1PcInstance tgPc = iter.next();
                if (tgPc.getAccountName().equalsIgnoreCase(accountName)) {
                    client.sendPacket(new S_CharSelectInfo(tgPc));
                }
            }
        } catch (final Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    }
}